﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Controls="clr-namespace:MahApps.Metro.Controls"
                    xmlns:Converters="clr-namespace:MahApps.Metro.Converters">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.TabControl.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <Converters:MetroTabItemCloseButtonWidthConverter x:Key="MetroTabItemCloseButtonWidthConverter" />

    <Style TargetType="{x:Type Controls:MetroTabItem}"
           BasedOn="{StaticResource MetroTabItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Controls:MetroTabItem}">
                    <Border x:Name="Border"
                            HorizontalAlignment="Stretch"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            Background="{TemplateBinding Background}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                        <StackPanel Orientation="Vertical"
                                    HorizontalAlignment="Stretch">
                            <StackPanel Orientation="Horizontal">
                                <ContentPresenter x:Name="ContentSite"
                                                  Margin="2,1,2,1"
                                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                  TextElement.FontSize="{TemplateBinding Controls:ControlsHelper.HeaderFontSize}"
                                                  TextElement.FontStretch="{TemplateBinding Controls:ControlsHelper.HeaderFontStretch}"
                                                  TextElement.FontWeight="{TemplateBinding Controls:ControlsHelper.HeaderFontWeight}"
                                                  TextElement.Foreground="{TemplateBinding Foreground}"
                                                  ContentSource="Header"
                                                  RecognizesAccessKey="True" />
                                <Button x:Name="PART_CloseButton"
                                        Style="{DynamicResource ChromelessButtonStyle}"
                                        Background="{DynamicResource GrayNormalBrush}"
                                        Width="{TemplateBinding Controls:ControlsHelper.HeaderFontSize, Converter={StaticResource MetroTabItemCloseButtonWidthConverter}}"
                                        Height="{Binding RelativeSource={RelativeSource Self}, Path=Width, Mode=OneWay}"
                                        HorizontalAlignment="Right"
                                        VerticalAlignment="Top"
                                        Margin="0 0 2 0"
                                        IsTabStop="False"
                                        Command="{TemplateBinding CloseTabCommand}"
                                        CommandParameter="{TemplateBinding CloseTabCommandParameter}">
                                    <Button.Resources>
                                        <Canvas x:Key="tabitem_close"
                                                Width="76"
                                                Height="76"
                                                Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
                                            <Path Width="31.6666"
                                                  Height="31.6667"
                                                  Canvas.Left="22.1666"
                                                  Canvas.Top="22.1667"
                                                  Stretch="Fill"
                                                  Fill="{DynamicResource BlackBrush}"
                                                  Data="F1 M 26.9166,22.1667L 37.9999,33.25L 49.0832,22.1668L 53.8332,26.9168L 42.7499,38L 53.8332,49.0834L 49.0833,53.8334L 37.9999,42.75L 26.9166,53.8334L 22.1666,49.0833L 33.25,38L 22.1667,26.9167L 26.9166,22.1667 Z " />
                                        </Canvas>
                                    </Button.Resources>
                                    <Button.OpacityMask>
                                        <VisualBrush Stretch="Uniform"
                                                     Visual="{StaticResource tabitem_close}" />
                                    </Button.OpacityMask>
                                </Button>
                            </StackPanel>
                            <Rectangle x:Name="Underline"
                                       Margin="0, 1, 0, 0"
                                       Height="2"
                                       HorizontalAlignment="Stretch"
                                       Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}, 
                                           Path=(Controls:TabControlHelper.IsUnderlined), Converter={StaticResource BooleanToVisibilityConverter}}" />
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected"
                                 Value="true">
                            <Setter Property="TextElement.Foreground"
                                    TargetName="ContentSite"
                                    Value="{DynamicResource AccentColorBrush}" />
                            <Setter Property="Fill"
                                    TargetName="Underline"
                                    Value="{DynamicResource AccentColorBrush}" />
                        </Trigger>
                        <Trigger Property="IsSelected"
                                 Value="false">
                            <Setter Property="TextElement.Foreground"
                                    TargetName="ContentSite"
                                    Value="{DynamicResource GrayNormalBrush}" />
                            <Setter Property="Fill"
                                    TargetName="Underline"
                                    Value="{DynamicResource GrayNormalBrush}" />
                        </Trigger>
                        <Trigger SourceName="ContentSite"
                                 Property="IsMouseOver"
                                 Value="True">
                            <Setter Property="TextElement.Foreground"
                                    TargetName="ContentSite"
                                    Value="{DynamicResource GrayHoverBrush}" />
                            <Setter Property="Fill"
                                    TargetName="Underline"
                                    Value="{DynamicResource GrayHoverBrush}" />
                            <Setter TargetName="PART_CloseButton"
                                    Property="Background"
                                    Value="{DynamicResource GrayHoverBrush}" />
                        </Trigger>
                        <Trigger SourceName="PART_CloseButton"
                                 Property="IsMouseOver"
                                 Value="True">
                            <Setter TargetName="PART_CloseButton"
                                    Property="Background"
                                    Value="{DynamicResource AccentColorBrush}" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver"
                                           SourceName="ContentSite"
                                           Value="True" />
                                <Condition Property="IsSelected"
                                           Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="TextElement.Foreground"
                                    TargetName="ContentSite"
                                    Value="{DynamicResource HighlightBrush}" />
                            <Setter Property="Fill"
                                    TargetName="Underline"
                                    Value="{DynamicResource HighlightBrush}" />
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>